package com.hao.wms.mvc;

import com.hao.wms.domain.OrderBill;
import com.hao.wms.query.OrderBillQueryObject;
import com.hao.wms.service.IBrandService;
import com.hao.wms.service.IOrderBillService;
import com.hao.wms.service.IProductService;
import com.hao.wms.service.ISupplierService;
import com.hao.wms.util.RequiredPermission;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import java.io.IOException;

public class OrderBillAction extends BaseAction{
	/**
	 * 
	 */
	private IOrderBillService orderBillService;
	private ISupplierService supplierService;

	public void setSupplierService(ISupplierService supplierService) {
		this.supplierService = supplierService;
	}


	private OrderBill orderBill = new OrderBill();
	
	public OrderBill getOrderBill() {
		return orderBill;
	}
	public void setOrderBill(OrderBill orderBill) {
		this.orderBill = orderBill;
	}
	private OrderBillQueryObject queryObject = new OrderBillQueryObject();
	
	
	public OrderBillQueryObject getQueryObject() {
		return queryObject;
	}
	public void setQueryObject(OrderBillQueryObject queryObject) {
		this.queryObject = queryObject;
	}
	public void setOrderBillService(IOrderBillService orderBillService) {
		this.orderBillService = orderBillService;
	}
	@InputConfig(methodName = "input")
	@RequiredPermission(value="OrderBill列表")
	public String execute() {
		putContext(PAGERESULT, orderBillService.query(queryObject));
		putContext("suppliers",supplierService.list());
		return LIST;
	}
	@RequiredPermission(value="新增/修改OrderBill")
	public String input() {
		putContext("suppliers",supplierService.list());
		if(orderBill.getId()!=null) {
			orderBill = orderBillService.get(orderBill.getId());
		}
		return INPUT;
	}
	@RequiredPermission(value="修改OrderBill")
	public String saveOrUpdate() {
		try {
			if(orderBill.getId()!=null) {
                orderBillService.update(orderBill);
                addActionMessage("修改成功");
            }else {
                orderBillService.save(orderBill);
                addActionMessage("保存成功");
            }
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	public void prepareSaveOrUpdate(){
		if(orderBill.getId()!=null) {
			orderBill = orderBillService.get(orderBill.getId());
			orderBill.setSupplier(null);
			orderBill.getItems().clear();//更新时使明细成为孤儿
		}
	}
	@RequiredPermission(value="删除OrderBill")
	public String delete() throws IOException{
		orderBill = orderBillService.get(orderBill.getId());
		orderBillService.delete(orderBill);
		putResponseText("删除成功");
		return NONE;
	}
	@RequiredPermission(value = "订单审核")
	public String audit(){
		if(orderBill.getId()!=null){
			orderBillService.audit(orderBill.getId());
			addActionMessage("审核成功");
		}
		return SUCCESS;
	}
	@RequiredPermission(value = "查看订单")
	public String show(){
		if(orderBill.getId()!=null) {
			orderBill = orderBillService.get(orderBill.getId());
		}
		return "show";
	}

	

}
